package com.guo.todo.usercenter.controller;

import com.alibaba.fastjson2.JSONObject;
import com.guo.todo.common.credential.Credential;
import com.guo.todo.common.excepiton.CommonException;
import com.guo.todo.common.response.Result;
import com.guo.todo.usercenter.service.credential.CredentialService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Auther: hotlove_linx
 * @Date: 2023/7/19 22:23
 * @Description:
 */
@RestController
@RequestMapping("/credential")
public class CredentialController {

    @Resource
    private CredentialService credentialService;

    @PostMapping("/login")
    public Result login(String name, String password, int terminal) {
        try {
            Credential credential = credentialService.loginProfile(name, password, terminal);
            return Result.success(credential);
        } catch (CommonException e) {
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/valid")
    public Result validSession(String token, int terminal) {
        try {
            credentialService.validSession(token, terminal);
        } catch (CommonException e) {
            return Result.fail(false, e.getMessage());
        }
        return Result.success(true);
    }
}
